<template>
  <div>
    <el-card>
      <el-form ref="form" class="box-card" :model="form" :inline="true">
        <div class="formtop">
          <el-form-item label="起始">
            <el-date-picker v-model="form.start_time" size="mini" type="date" placeholder="选择日期" style="width: 100%;" />
          </el-form-item>
          <el-form-item label="截至">
            <el-date-picker v-model="form.end_time" size="mini" type="date" placeholder="选择日期" style="width: 100%;" />
          </el-form-item>
          <el-form-item label="分类">
            <el-select
              v-model="form.param_id"
              multiple
              collapse-tags
              style="width: 100%;"
              clearable
              filterable
              size="mini"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" label-width="0">
            <el-button class="h50" size="mini" type="primary" @click="getChartData">搜索</el-button>
          </el-form-item>
        </div>

        <el-form-item label="" label-width="0">
          <!-- tab 类型 -->
          <div class="formRight">
            <template v-for="item in tabData">
              <!-- eslint-disable-next-line -->
              <div
                style="cursor: pointer;"
                :class="item.id === form.type_id ? 'active' : ''"
                @click="handleType(item.id)"
              >{{ item.type_name }}
              </div>
            </template>
          </div>
        </el-form-item>
      </el-form>
      <el-divider />
      <div id="myChart1" class="chart" />

      <el-divider />
      <!--  表格数据  -->
      <div class="tableBox">
        <el-table
          :data="tableData"
          border
          style="width: 100%;"
        >
          <el-table-column
            prop="name"
            label="采集因子"
            align="center"
          />
          <el-table-column
            prop="max"
            label="最大值(℃)"
            align="center"
          />
          <el-table-column
            prop="max_time"
            label="最大时间"
            align="center"
          />
          <el-table-column
            prop="min"
            label="最小值(℃)"
            align="center"
          />
          <el-table-column
            prop="min_time"
            label="最小时间"
            align="center"
          />
          <el-table-column
            prop="average"
            label="平均值(℃)"
            align="center"
          />
        </el-table>
      </div>
    </el-card>
  </div>
</template>
<script>
import { electricityParameters, monitorChartData } from '@/api/realTime.js'

export default {
  name: 'content',
  components: {},
  props: {
    choseData: {
      type: Object,
      required: {}
    }
  },
  data() {
    return {
      tabData: [], // tab数据
      typeId: null, // tabId
      tableData: [],
      options: [],
      form: {
        start_time: '',
        end_time: '',
        monitor_id: '',
        param_id: [],
        type_id: 0
      },
      //  图表数据
      option: {
        title: {
          text: ''
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {},
        toolbox: {
          show: true,
          feature: {
            dataZoom: {
              yAxisIndex: 'none'
            },
            dataView: { readOnly: false },
            magicType: { type: ['line', 'bar'] },
            restore: {},
            saveAsImage: {
              show: true,
              icon: 'M4.7,22.9L29.3,45.5L54.7,23.4M4.6,43.6L4.6,58L53.8,58L53.8,43.6M29.2,45.1L29.2,0',
              title: '保存为图片',
              type: 'png',
              connectedBackgroundColor: '#fff',
              name: '',
              excludeComponents: ['toolbox'],
              lang: ['右键另存为图片']
            }
          }
        },
        axisPointer: {
          link: [
            {
              xAxisIndex: 'all'
            }
          ]
        },
        dataZoom: [
          {
            type: 'inside',
            start: 0,
            end: 100
          },
          {
            start: 0,
            end: 100
          }
        ],
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: []
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            formatter: '{value} °C'
          }
        },
        series: []
      }
    }
  },
  watch: {
    // 树形数据
    choseData(nVal, oVal) {
      this.form.monitor_id = nVal.id
      this.getTopSearch()
    }
  },
  created() {
    // 获取当前时间
    this.form.start_time = this.getDay(-1)
    this.form.end_time = this.getDay(0)
  },
  mounted() {
    this.drawLine()
  },
  methods: {
    //   获取图表数据
    getChartData() {
      this.flag = false
      monitorChartData(this.form).then(res => {
        this.option.xAxis.data = res.data.xData
        this.tableData = res.data.tableData
        this.option.legend.data = res.data.legend
        const arr = [] // res.data.series
        res.data.series.forEach(item => {
          const obj = {}
          obj.data = item.data
          obj.name = item.name
          obj.type = 'line'
          obj.markPoint = {
            data: [
              { type: 'max', name: 'Max' },
              { type: 'min', name: 'Min' }
            ]
          }
          obj.markLine = {
            data: [{ type: 'average', name: 'Avg' }]
          }
          arr.push(obj)
        })
        this.$set(this.option, 'series', arr)
        this.drawLine()
      })
    },
    // 获取顶部参数查询
    getTopSearch() {
      electricityParameters(this.choseData.id, this.form.type_id).then(res => {
        this.tabData = res.data.data
        this.options = res.data.child
        this.form.param_id = []
        this.options.forEach(item => {
          this.form.param_id.push(item.id)
        })
        if (this.tabData.length > 0 && this.form.type_id === 0) {
          this.form.type_id = this.tabData[0].id
          this.getChartData()
        }
      })
    },
    // 初始化表格
    drawLine() {
      this.$echarts.dispose(document.getElementById('myChart1'))
      // 基于准备好的dom，初始化echarts实例
      const myChart1 = this.$echarts.init(document.getElementById('myChart1'))
      // 绘制图表
      myChart1.setOption(this.option, true)
      window.addEventListener('resize', () => {
        myChart1.resize()
      })
    },
    /**
     * @description: 获取当前日期
     * @author: hst
     * @param: day: 0今天，-1昨天
     */
    getDay(day) {
      var today = new Date()
      var targetday = today.getTime() + 1000 * 60 * 60 * 24 * day
      today.setTime(targetday)
      var tYear = today.getFullYear()
      var tMonth = today.getMonth()
      var tDate = today.getDate()
      tMonth = this.doHandleMonth(tMonth + 1)
      tDate = this.doHandleMonth(tDate)
      return tYear + '-' + tMonth + '-' + tDate
    },
    doHandleMonth(month) {
      var m = month
      if (month.toString().length === 1) {
        m = '0' + month
      }
      return m
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .tableHead {
  background-color: #EDF1F8 !important;
}

.h50 {
  height: 28px;
}

.box-card {
  /*height: 30px;*/
}

.formtop {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  height: 50px;
}
.formRight {
  width: 100%;
  overflow-x: auto;
  margin-top: 10px;
  margin-bottom: 10px;

  div {
    float: left;
    height: 35px;
    margin: 0 10px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #838ea0;
  }

  .active {
    height: 35px;
    background: linear-gradient(90deg, #3494ff, #006dff);
    //box-shadow: 0px 6px 12px 0px rgba(35, 113, 255, 0.29);
    border-radius: 17px;
    color: #ffffff;
    padding: 0px 25px;
  }
}

.chart {
  width: 100%;
  height: 480px;
}

.tableBox {
  width: 1140px;
  margin: 20px auto;
}

.warningBox {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: 1140px;
  height: 54px;
  border: 1px solid #cecdcd;
  background: #f1f1f1;
  border-radius: 10px;
  font-size: 14px;
  padding: 15px;
  margin: 10px auto;

  .left {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    background-color: #333333;
    margin-right: 10px;
  }

  .right {
    font-size: 14px;
  }
}
</style>
